package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class CriarBanco {

  static  String url   = "jdbc:mysql://localhost/testebd";
  static  String user  = "root";
  static  String senha = "root";

    
    public static void main( String args[] ) {
        
        String creat_schema = "create schema if not exists AV2";
        String use_schema   = "use crud;";
        String drop_table   = "drop table if exists contatos";
        String creat_table  = " create table `contatos` ( \n"
                            + "`id` int(10) unsigned not null auto_increment, \n"
                            + "`nome` varchar(45) not null, \n"
                            + "`email` varchar(45) not null, \n"
                            + "`endereco` varchar(45) not null, \n"
                            + " primary key( id ) )";
                
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection( url , user, senha);
            
            PreparedStatement stmt1 = con.prepareStatement( creat_schema);
            PreparedStatement stmt2 = con.prepareStatement( use_schema  );
            PreparedStatement stmt3 = con.prepareStatement( drop_table  );
            PreparedStatement stmt4 = con.prepareStatement( creat_table );
            
            stmt1.execute( );
            stmt2.execute( );
            stmt3.execute( );
            stmt4.execute( );
            
            stmt1.close( );
            stmt2.close( );
            stmt3.close( );
            stmt4.close( );
            
            JOptionPane.showMessageDialog( null, "Schema criado com sucesso !" );
            
        } catch (SQLException e) {
            
            JOptionPane.showMessageDialog( null, "Erro ao criar o schema: " + e );
                                    
        }  catch (ClassNotFoundException e) {
            
            JOptionPane.showMessageDialog( null, "Erro ao criar o schema: " + e );
            
        }
    }
}